package com.wmx.db2doc.util;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * 在 Word 中创建表格工具类
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2020/12/29 21:15
 */
public class WordTableUtils {

    /**
     * Word 文档中创建表格
     *
     * @param xwpfDocument ：XWPFDocument 是用于处理 .docx 文件的高级 API
     * @param headerTitle  ：表头文本内容
     * @param dataList     ：表格正文数据
     * @throws Exception
     */
    public static void createSimpleTable(XWPFDocument xwpfDocument, String[] headerTitle, List<Map<String, Object>> dataList) {

        //创建一个指定了行和列的空表
        XWPFTable table = xwpfDocument.createTable(dataList.size() + 1, dataList.get(0).size());
        //设置表格对齐方式
        table.setTableAlignment(TableRowAlign.LEFT);
        //设置表格宽度为 100%
        table.setWidth("100%");

        //设置表头
        setHeaderRow(table, headerTitle);

        int bodyRowSize = dataList.size();
        int bodyColSize = dataList.get(0).size();

        for (int i = 0; i < bodyRowSize; i++) {
            Map<String, Object> objectMap = dataList.get(i);
            for (int j = 0; j < bodyColSize; j++) {
                Object[] objects = objectMap.values().toArray();
                if (j < objects.length && objects[j] != null) {
                    table.getRow(i + 1).getCell(j).setText(objects[j].toString());
                }
            }
        }
    }

    /**
     * 设置表头
     *
     * @param table
     */
    private static void setHeaderRow(XWPFTable table, String[] headerTitle) {
        int cellIndex = 0;
        for (String title : headerTitle) {
            XWPFTableCell headCell = table.getRow(0).getCell(cellIndex++);
            headCell.setColor("BFC6D1");
            headCell.setText(title);
        }
    }

    /**
     * 自定义标题样式
     *
     * @param docxDocument ：目标文档
     * @param strStyleId   ：样式id，后期正文中的标题文本会通过样式id进行关联，而且样式名称会显示在文档的标题样式中
     * @param headingLevel ：样式级别,从0开始，一级标题是0，二级标题是1，依此类推
     */
    public static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {

        CTStyle ctStyle = CTStyle.Factory.newInstance();
        ctStyle.setStyleId(strStyleId);

        CTString styleName = CTString.Factory.newInstance();
        styleName.setVal(strStyleId);
        ctStyle.setName(styleName);

        CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
        indentNumber.setVal(BigInteger.valueOf(headingLevel));

        // lower number > style is more prominent in the formats bar
        ctStyle.setUiPriority(indentNumber);

        CTOnOff onoffnull = CTOnOff.Factory.newInstance();
        ctStyle.setUnhideWhenUsed(onoffnull);

        // style shows up in the formats bar
        ctStyle.setQFormat(onoffnull);

        // style defines a heading of the given level
        CTPPr ppr = CTPPr.Factory.newInstance();
        ppr.setOutlineLvl(indentNumber);
        ctStyle.setPPr(ppr);

        XWPFStyle style = new XWPFStyle(ctStyle);

        // is a null op if already defined
        XWPFStyles styles = docxDocument.createStyles();

        style.setType(STStyleType.PARAGRAPH);
        styles.addStyle(style);
    }
}

